 <template>
  <div>
    <!-- 卡片组件 -->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <!-- 标签选项卡组件 -->
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <!-- 选项页标签 -->
          <el-tab-pane label="最新主题" name="latest">
            <!-- 对帖子列表进行for循环 -->
            <article
              v-for="(item, index) in articleList"
              :key="index"
              class="media"
            >
              <!-- 对循环出的每个帖子分为左部分和中间部分（BULMA中Documentation中Layout） -->
              <!-- 头像 -->
              <div class="media-left">
                <figure class="image is-48x48">
                  <img
                    :src="`https://cn.gravatar.com/avatar/${item.userId}?s=164&d=monsterid`"
                    style="border-radius: 5px"
                  />
                </figure>
              </div>
              <!-- 帖子标题和内容 -->
              <div class="media-content">
                <div class="">
                  <p class="ellipsis is-ellipsis-1">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      :content="item.title"
                      placement="top"
                    >
                      <!-- name传组件名，params传递参数 -->
                      <router-link :to="{ name: 'post-detail', params: { id: item.id } }">
                        <span class="is-size-6">{{ item.title }}</span>
                      </router-link>
                    </el-tooltip>
                  </p>
                </div>
                <!-- 用户别名 -->
                <nav class="level has-text-grey is-mobile is-size-7 mt-2">
                  <div class="level-left">
                    <div class="level-left">
                      <router-link class="level-item" :to="{ path: `/member/${item.username}/home` }">
                        {{ item.alias }}
                      </router-link>

                      <span class="mr-1"
                        >发布于:{{
                          dayjs(item.createTime).format("YYYY/MM/DD")
                        }}</span
                      >
                      <!-- 帖子标签 -->
                      <span
                        v-for="(tag, index) in item.tags"
                        :key="index"
                        class="tag is-hidden-mobile is-success is-light mr-1"
                      >
                        <router-link
                          :to="{ name: 'tag', params: { name: tag.name } }"
                        >
                          {{ "#" + tag.name }}
                        </router-link>
                      </span>

                      <span class="is-hidden-mobile">浏览:{{ item.view }}</span>
                    </div>
                  </div>
                </nav>
              </div>
              <div class="media-right" />
            </article>
          </el-tab-pane>
          <el-tab-pane label="热门主题" name="hot">
            <article
              v-for="(item, index) in articleList"
              :key="index"
              class="media"
            >
              <div class="media-left">
                <figure class="image is-48x48">
                  <img
                    :src="`https://cn.gravatar.com/avatar/${item.userId}?s=164&d=monsterid`"
                    style="border-radius: 5px"
                  />
                </figure>
              </div>
              <div class="media-content">
                <div class="">
                  <p class="ellipsis is-ellipsis-1">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      :content="item.title"
                      placement="top"
                    >
                      <router-link
                        :to="{ name: 'post-detail', params: { id: item.id } }"
                      >
                        <span class="is-size-6">{{ item.title }}</span>
                      </router-link>
                    </el-tooltip>
                  </p>
                </div>
                <nav class="level has-text-grey is-mobile is-size-7 mt-2">
                  <div class="level-left">
                    <div class="level-left">
                      <router-link
                        class="level-item"
                        :to="{ path: `/member/${item.username}/home` }"
                      >
                        {{ item.alias }}
                      </router-link>

                      <span class="mr-1">
                        发布于:{{ dayjs(item.createTime).format("YYYY/MM/DD") }}
                      </span>

                      <span
                        v-for="(tag, index) in item.tags"
                        :key="index"
                        class="tag is-hidden-mobile is-success is-light mr-1"
                      >
                        <router-link
                          :to="{ name: 'tag', params: { name: tag.name } }"
                        >
                          {{ "#" + tag.name }}
                        </router-link>
                      </span>

                      <span class="is-hidden-mobile">浏览:{{ item.view }}</span>
                    </div>
                  </div>
                </nav>
              </div>
              <div class="media-right" />
            </article>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!--大于0就显示分页组件 -->
      <pagination
        v-show="page.total > 0"
        :total="page.total"
        :page.sync="page.current"
        :limit.sync="page.size"
        @pagination="init"
      />
      <!-- pagination事件绑定到init函数 -->

    </el-card>
  </div>
</template>

<script>
import { getList } from "@/api/post";
import Pagination from '@/components/Pagination'

export default {
  name: "TopicList",
  components: { Pagination },
  data() {
    return {
      activeName: "latest", //帖子类型：最新/最热
      articleList: [], //帖子列表
      page: {
        current: 1,
        size: 5,
        total: 0,
        tab: 'latest'
      }
    };
  },
  //页面加载完成就加载全部帖子
  created() {
    this.init(this.tab);
  },
  methods: {
    //向服务器发请求取最新的当前页数据
    init(tab) {
      // 第一页的10个最新帖子
        getList(this.page.current, this.page.size, tab).then((response) => {
        const { data } = response;
        this.page.current = data.current
        this.page.total = data.total
        this.page.size = data.size
        this.articleList = data.records;
      });
    },
    handleClick(tab) {
      this.init(tab.name);
    },
  },
};
</script>

<style scoped>
</style>